package org.jeecg.modules.basic.repair.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basic.repair.entity.LwgyFixedAssets;
import org.jeecg.modules.basic.repair.entity.PropertyReport;
import org.jeecg.modules.basic.repair.service.ILwgyFixedAssetsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 固定资产
 * @Author: jeecg-boot
 * @Date:   2023-02-08
 * @Version: V1.0
 */
@Api(tags="固定资产")
@RestController
@RequestMapping("/property/lwgyFixedAssets")
@Slf4j
public class LwgyFixedAssetsController extends JeecgController<LwgyFixedAssets, ILwgyFixedAssetsService> {
	@Autowired
	private ILwgyFixedAssetsService lwgyFixedAssetsService;


	/**
	 * 分页列表查询
	 *
	 * @param lwgyFixedAssets
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "固定资产-分页列表查询")
	@ApiOperation(value="固定资产-分页列表查询", notes="固定资产-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyFixedAssets lwgyFixedAssets,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyFixedAssets> queryWrapper = QueryGenerator.initQueryWrapper(lwgyFixedAssets, req.getParameterMap());


		Page<LwgyFixedAssets> page = new Page<>(pageNo, pageSize);
		IPage<LwgyFixedAssets> pageList = lwgyFixedAssetsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "固定资产-添加")
	@ApiOperation(value="固定资产-添加", notes="固定资产-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyFixedAssets lwgyFixedAssets) {
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			lwgyFixedAssets.setUserId(sysUser.getUsername());
			lwgyFixedAssets.setUserTime((new Date()));
			lwgyFixedAssetsService.save(lwgyFixedAssets);
		    return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param lwgyFixedAssets
	 * @return
	 */
	@AutoLog(value = "固定资产-编辑")
	@ApiOperation(value="固定资产-编辑", notes="固定资产-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyFixedAssets lwgyFixedAssets) {
		lwgyFixedAssets.setScrapDate(new Date());
		lwgyFixedAssetsService.updateById(lwgyFixedAssets);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "固定资产-通过id删除")
	@ApiOperation(value="固定资产-通过id删除", notes="固定资产-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyFixedAssetsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "固定资产-批量删除")
	@ApiOperation(value="固定资产-批量删除", notes="固定资产-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyFixedAssetsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "固定资产-通过id查询")
	@ApiOperation(value="固定资产-通过id查询", notes="固定资产-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyFixedAssets lwgyFixedAssets = lwgyFixedAssetsService.getById(id);
		if(lwgyFixedAssets==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyFixedAssets);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyFixedAssets lwgyFixedAssets) {
		return super.exportXls(request, lwgyFixedAssets, LwgyFixedAssets.class, "固定资产");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyFixedAssets.class);
    }


	 /**统计-校区*/
	 @PostMapping("/findPropertyReport")
	 @ResponseBody
	 public Result<?>  findPropertyReport(){
		 List<PropertyReport> reportList = lwgyFixedAssetsService.findPropertyRecordList();
		 System.out.println("1111113323:"+reportList);
		 IPage<PropertyReport>reportIPage = new Page<PropertyReport>(0,100);
		 reportIPage.setRecords(reportList);
		 return Result.OK(reportIPage);
	 }
	 /**统计-楼宇*/
	 @PostMapping("/findPropertyReport2")
	 @ResponseBody
	 public Result<?>  findPropertyReport2(){
		 List<PropertyReport> reportList = lwgyFixedAssetsService.findPropertyRecord2List();
		 IPage<PropertyReport>reportIPage = new Page<PropertyReport>(0,100);

		 reportIPage.setRecords(reportList);
		 return Result.OK(reportIPage);
	 }


}
